/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package brigal.fastbackup.data;

import brigal.xml.XMLTools;
import brigal.xml.XPathRequester;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 *
 * @author brigal
 */
public class Profile {

    private String name;
    private String source;
    private String destination;

    public Profile(String name, String sourcePath, String destinationPath) {
        this.name = name;
        this.source = sourcePath;
        this.destination = destinationPath;
    }

    public Profile(String xmlData) {
        this.fromXML(xmlData);
    }

    public void setName(String profileName) {
        this.name = profileName;
    }

    public String getName() {
        return this.name;
    }

    public void setSource(String sourcePath) {
        this.source = sourcePath;
    }

    public String getSource() {
        return this.source;
    }

    public void setDestination(String destinationPath) {
        this.destination = destinationPath;
    }

    public String getDestination() {
        return this.destination;
    }

    public void fromXML(String xmlData) {
        XPathRequester xpr = new XPathRequester(xmlData);
        this.name = xpr.selectSingleNodeText("/Profile/@Name");
        this.source = xpr.selectSingleNodeText("/Profile/Source");
        this.destination = xpr.selectSingleNodeText("/Profile/Destination");
    }

    public Document toXMLDocument() {
        Document doc = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.newDocument();

            this.appendXML(doc, doc);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Profile.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return doc;
        }
    }

    public void appendXML(Document appendToThisDocument, Node startingFromThisNode) {
        Element eltProfile = appendToThisDocument.createElement("Profile");
        startingFromThisNode.appendChild(eltProfile);
        Node attrName = appendToThisDocument.createAttribute("Name");
        attrName.setNodeValue(this.name);
        eltProfile.getAttributes().setNamedItem(attrName);
        // <Source>
        Element eltSource = appendToThisDocument.createElement("Source");
        eltSource.appendChild(appendToThisDocument.createTextNode(this.source));
        eltProfile.appendChild(eltSource);
        // <Destination>
        Element eltDestination = appendToThisDocument.createElement("Destination");
        eltDestination.appendChild(appendToThisDocument.createTextNode(this.destination));
        eltProfile.appendChild(eltDestination);
    }

    public String toXMLString() {
        return XMLTools.toXMLString(this.toXMLDocument());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 53 * hash + (this.source != null ? this.source.hashCode() : 0);
        hash = 53 * hash + (this.destination != null ? this.destination.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof Profile) {
            Profile anotherProfile = (Profile) anObject;
            if (this.name.equalsIgnoreCase(anotherProfile.name) && this.source.equalsIgnoreCase(anotherProfile.source) && this.destination.equalsIgnoreCase(anotherProfile.destination)) {
                return true;
            }
        }
        return false;
    }
}
